package com.jeejio.train.controller;

import com.jeejio.train.entity.Lecturer;
import com.jeejio.train.service.LecturerService;
import com.jeejio.train.util.IConstant;
import com.jeejio.train.util.UserException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/lecturer")
public class LecturerController {

    @Autowired
    private LecturerService lecturerService = null;

    @Autowired
    private HttpServletRequest httpServletRequest = null;

    @RequestMapping("/login")
    public String login(@RequestParam("name") String name
            , @RequestParam("pwd") String pwd) {
        JSONObject jResult = new JSONObject();
        try {
            Lecturer lecturer = lecturerService.login(name, pwd);
            if (null != lecturer) {
                // Create session.
                HttpSession httpSession = httpServletRequest.getSession();
                httpSession.setMaxInactiveInterval(3600); // second.
                httpSession.setAttribute(IConstant.KEY_SESSION_USER, lecturer);

                jResult.put("state", IConstant.SUCCESS);
                jResult.put("msg", "登录成功");
                return jResult.toString();
            }
        } catch (UserException e) {
            e.printStackTrace();
            jResult.put("state", e.getErrorCode());
            jResult.put("msg", e.getMessage());
            return jResult.toString();
        }
        jResult.put("state", IConstant.E_LOGIN_INVALID_NAME_AND_PWD);
        jResult.put("msg", "用户名或密码错误");
        return jResult.toString();
    }

    @RequestMapping("/is_login")
    public String isLogin() {
        JSONObject jResult = new JSONObject();
        HttpSession httpSession = httpServletRequest.getSession(false);
        Lecturer lecturer = null;
        if ((null != httpSession)
                && (null != (lecturer = (Lecturer)
                httpSession.getAttribute(IConstant.KEY_SESSION_USER)))) {
            jResult.put("is_login", true);
            jResult.put("login_name", lecturer.getLoginName());
            jResult.put("full_name", lecturer.getFullName());
        } else {
            jResult.put("is_login", false);
        }
        return jResult.toString();
    }

    @RequestMapping("/logout")
    public String logout() {
        HttpSession httpSession = httpServletRequest.getSession(false);
        if (null != httpSession) {
            if (null != httpSession.getAttribute(IConstant.KEY_SESSION_USER)) {
                httpSession.removeAttribute(IConstant.KEY_SESSION_USER);
            }
            httpSession.invalidate();
        }
        return "{\"state\": " + IConstant.SUCCESS + "}";
    }
}
